﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Data.SqlClient;

namespace SimpoBudgetDataModel
{
    public class AccountList : Collection<Account>
    {
        public AccountList Load()
        {
            AccountList _accountList = new AccountList();
            Account _account;

            string connectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=SimpoBudget;Integrated Security=True;MultipleActiveResultSets=True";
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                // Do work here.
                SqlCommand sqlSelect = new SqlCommand("select * from account", connection);
                SqlDataReader accountSQLDataReader = sqlSelect.ExecuteReader();
                while (accountSQLDataReader.Read())
                {
                    _account = new Account();
                    _account.Id = accountSQLDataReader.GetFieldValue<int>(0);
                    _account.Name = accountSQLDataReader.GetFieldValue<string>(1);
                    _account.Balance = accountSQLDataReader.GetFieldValue<decimal>(2);
                    _account.Url = accountSQLDataReader.GetFieldValue<string>(3);
                    _accountList.Add(_account);
                }
            }

            return _accountList;
        }

        public event EventHandler<AccountChangedEventArgs> Changed;

        protected override void InsertItem(int index, Account Account)
        {
            base.InsertItem(index, Account);

            EventHandler<AccountChangedEventArgs> temp = Changed;
            if (temp != null)
            {
                temp(this, new AccountChangedEventArgs(
                    ChangeType.Added, Account, null));
            }
        }

        protected override void SetItem(int index, Account Account)
        {
            Account replaced = Items[index];
            base.SetItem(index, Account);

            EventHandler<AccountChangedEventArgs> temp = Changed;
            if (temp != null)
            {
                temp(this, new AccountChangedEventArgs(
                    ChangeType.Replaced, replaced, Account));
            }
        }

        protected override void RemoveItem(int index)
        {
            Account removedItem = Items[index];
            base.RemoveItem(index);

            EventHandler<AccountChangedEventArgs> temp = Changed;
            if (temp != null)
            {
                temp(this, new AccountChangedEventArgs(
                    ChangeType.Removed, removedItem, null));

            }
        }

        protected override void ClearItems()
        {

            base.ClearItems();

            EventHandler<AccountChangedEventArgs> temp = Changed;
            if (temp != null)
            {
                temp(this, new AccountChangedEventArgs(ChangeType.Cleared, null, null));

            }
        }

        public class AccountChangedEventArgs : EventArgs
        {
            public readonly Account ChangedItem;
            public readonly ChangeType ChangeType;
            public readonly Account ReplacedWith;

            public AccountChangedEventArgs(ChangeType change, Account Account, Account replacement)
            {
                ChangeType = change;
                ChangedItem = Account;
                ReplacedWith = replacement;
            }
        }
        
        public enum ChangeType
        {
            Added,
            Removed,
            Replaced,
            Cleared
        };

    }
}
